<?php

namespace Admin\Model;

use Think\Model;

/**
 * 模型类类系统菜单模型类
 *
 * 系统菜单相关操作
 *
 * @author: xuhui <xuh86@126.com>
 * @version: $Id$
 * @package: asoc.Admin.Model
 *
 */
class SysMenuModel extends Model {
	
	
	/**
	 * 根据管理员权限获取系统菜单
	 * @return array
	 */
	public function getMenuList($cache=false){
		
		$menus		=	NULL;
		
		if($cache){
			
			if(session("?sysmenu"))	return session("sysmenu");
		
		}
		
		
		$adminId	=	session('?'.C('BE_UID')) ? session(C('BE_UID')) : 0;

		if($adminId){
			
			$map['is_hidden']	=	0;
			$map['level']	=	1;
			$map['parant_id']	=	0;
			
			$topList	=	$this->where($map)->order("menu_order DESC,id ASC")->select();

			if($topList){
				
				
				$acModel	=	D('Access');
				
				foreach($topList as $key=>$val){
					
					//查看下级菜单用户拥有权限，当一个菜单下
					$subList	=	$this->where("parent_id=".$val['id']." AND is_hidden=0")->order("menu_order DESC,id ASC")->select();
					
					$subNum		=	0;		//下级菜单个数;
					$subMenu	=	NULL;	//子菜单
					if($subList){
						
						foreach ($subList as $k=>$v){
							
							//不需要检查权限
							if($v['is_check'] == 0 || $acModel->isAccess($adminId,$v['module'],$v['action'])){
								
								$subNum++;
								$subMenu[]	=	$v;
							}

						}
					}

					$menuItem		=	NULL;
					
				
						
					$menuItem	=	array(
						'id'	=>	$val['id'],
						'module'	=>	$val['module'],
						'action'	=>	$val['action'],
						'title'		=>	$val['title'],
						'level'		=>	$val['level'],
						'submenu'	=>	$subMenu,	
					);
					
					$menus[]	=	$menuItem;
					
					if($cache){
						//缓存配置选项
						$_SESSION['sysmenu'][]	=	$menuItem;
					}
				
				}
			}
		}
		
		return $menus;
		
	}
	
	
	/**
	 * 按一级菜单分组获取需要权限控制的节点
	 * @param mixed $is_check: 0不需要验证 1需要验证 all全部
	 * @param mixed $is_hidden: 0显示 1隐藏  all全部
	 * @return array
	 */
	public function getItemListByGroup($is_check=1,$is_hidden='all'){
		
		
		$map['parent_id']	=	0;
		$map['level']	=	1;
		
		$moduleList	=	$this->where($map)->order("menu_order DESC")->select();

		if($moduleList){
			
			foreach ($moduleList as $key=>$val){
				
				$condition['parent_id']	=	$val['id'];
				
				if($is_check != "all")	$condition['is_check']	=	$is_check;
				if($is_hidden != "all")	$condition['is_hidden']	=	$is_hidden;
				
				$moduleList[$key]['sub']	=	$this->where($condition)->order("menu_order DESC")->select();
			
			}
		}
		
		return $moduleList;
	}
	
	
	/**
	 * 根据查询条件返回分页数据
	 * 
	 * @param mixed $map: 查询过滤条件
	 * @param int $currentPage:当前页
	 * @param int $pageSize: 每页大小
	 * @param string $sortBy: 排序条件
	 * @return array 
	 */
	public function getItemListBy($map,$currentPage,$pageSize,$sortBy=''){

		$items	=	NULL;
		$count	=	0;			//总条数
		
	
		$sortBy	=	empty($sortBy) ? "menu_order DESC " : $sortBy;
		
		$items	=	$this->where($map)->order($sortBy)->page($currentPage.','.$pageSize)->select();
		
		$count	=	$this->where($map)->count();
		
		return array('items'=>$items,'count'=>$count);
	}
	
	
	
	/**
	 * 
	 * @param array $data:需要保存的数组
	 * @return array
	 */
	public function actionSave($data){
		
		$status		=	0;
		$msg		=	'';	
		
		$pk			=	$this->getPk();		//主键
		$fileds		=	$this->getDbFields();
		
		$adminId	=	session(C('BE_UID'));
		$admin		=	D('Admin');
		
		if(isset($data[$pk])&& $data[$pk]>0){		// 修改

			for($i=0;$i<count($fileds);$i++){
				
				$filed	=	$fileds[$i];
				if($filed != "module" && $filed != "parent_id" ){
					
					$vo[$filed]	=	$data[$filed];
						
				}
			}
			
			$this->where($pk."=".$data[$pk])->save($vo);
			
			//保存操作记录
			$admin->addAdminLog('修改后台菜单,id:'.$data[$pk],$adminId);
		
		}else{
			
			for($i=0;$i<count($fileds);$i++){
			
				$filed	=	$fileds[$i];
						
				$vo[$filed]	=	$data[$filed];
			}
				
			$newId		=	$this->add($vo);
			
			$status		=	2;
			$msg		=	"添加出错";
			if($newId>0){
				
				$status		=	1;
				$msg		=	"";
				//保存操作记录
				$admin->addAdminLog('新增后台菜单,id:'.$newId,$adminId);
			}

		}
		
		
		return array('status'=>$status,'msg'=>$msg);
	}
	
	
	/**
	 * 按条件删除菜单项
	 * @param mixed $condition: int/array 删除菜单的id
	 * @return int:成功删除记录数
	 */
	public function deleteBy($condition){
	
		$delNum		=	0;
		$adminId	=	session(C('BE_UID'));
		
		$admin		=	D('Admin');
		//删除单个
		if(is_numeric($condition)){
	
			if($this->find($condition)){
					
				$delNum++;
				$this->delete($condition);
					
				//保存操作记录
				$admin->addAdminLog('删除后台菜单,id:'.$condition,$adminId);
			}
		}
		//删除多个
		if(is_array($condition)){
				
			$num	=	count($condition);
				
			for($i=0;$i<$num;$i++){
	
				$recordId	=	$condition[$i];
				if($recordId>0){
						
					if($this->find($recordId)){
	
						$delNum++;
						$this->delete($recordId);
	
						//保存操作记录
						$admin->addAdminLog('删除后台菜单,id:'.$recordId,$adminId);
					}
						
				}
			}
				
		}
	
		return $delNum;
	}
	
	
	/**
	 * 获取指定操作对应的面包屑元素信息
	 * @param string $module: 当前模块 可选
	 * @param string $action: 当前操作 可选
	 * @return array
	 */
	public function getActionMeta($module='',$action=''){
		
		$metaData	=	NULL;
		
		$module		=	empty($module) ? CONTROLLER_NAME : $module;
		$action		=	empty($action) ? ACTION_NAME : $action;
		
		$actionInfo	=	$this->where(array('module'=>$module,'action'=>$action,'level'=>2))->find();
		
		if($actionInfo){
			
			$moduleInfo			=	$this->getModuleInfo($actionInfo['module']);
			
			$metaData['action']	=	$actionInfo['title'];
			$metaData['module']	=	$moduleInfo['title'];
			
			$metaData['listTitle']	=	$actionInfo['title'];
		}
		
		return $metaData;
	}
	
	
	
	/**
	 * 获取指定操作所属模块
	 * @param string $moduleName: 操作对应模块名称
	 * @return array 
	 */
	
	public function getModuleInfo($moduleName){
		
		$moduleInfo		=	NULL;
		
		if($moduleName){
			
			$moduleInfo		=	$this->where(array('module'=>$moduleName,'level'=>1,'parent_id'=>0))->find();
		
		}
		
		return $moduleInfo;
	}
}

?>